package com.yzl.meeting.business.controller;

import com.yzl.meeting.business.dto.ShowUser;
import com.yzl.meeting.business.entity.Delegation;
import com.yzl.meeting.business.entity.PageBean;
import com.yzl.meeting.business.entity.Staff;
import com.yzl.meeting.business.entity.User;
import com.yzl.meeting.business.rule.DataResult;
import com.yzl.meeting.business.rule.ResponseCode;
import com.yzl.meeting.business.service.Impl.MangeUserServiceImpl;
import com.yzl.meeting.business.service.MangeUserService;
import com.yzl.meeting.business.vo.NewUserVo;
import com.yzl.meeting.framework.annotation.Controller;
import com.yzl.meeting.framework.annotation.RequestBody;
import com.yzl.meeting.framework.annotation.RequestMapping;

import java.sql.SQLException;
import java.util.List;

/**
 * @Title: MangeUserController
 * @Author: 姚志立
 * @Description:
 * @Date: 2020/10/14 11:00
 * @Version: 1.0
 */

@Controller("/MangeUserController")
public class MangeUserController {
    MangeUserService mangeUserService = new MangeUserServiceImpl();

    @RequestMapping("/listDelegation")
    public DataResult<Delegation> listDelegation() {
        DataResult dataResult = null;
        try {
            List<Delegation> delegations = mangeUserService.listDelegation();
            dataResult = new DataResult<>(delegations);
        } catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }

    @RequestMapping("/listAllStaff")
    public DataResult<Staff> listStaff() {
        DataResult dataResult = null;
        try {
            List<Staff> staffs = mangeUserService.listAllStaff();
            dataResult = new DataResult<>(staffs);
        } catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }
    @RequestMapping("/listStaff")
    public DataResult<Staff> listStaff(@RequestBody Delegation delegation) {
        DataResult dataResult = null;
        try {
            Integer delegationId = delegation.getDelegationId();
            List<Staff> staffs = mangeUserService.listStaff(delegationId);
            dataResult = new DataResult<>(staffs);
        } catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }

    @RequestMapping("/addUser")
    public DataResult<Staff> addUser(@RequestBody NewUserVo newUserVo) {
        DataResult dataResult = null;
        try {
            mangeUserService.addUser(newUserVo);
                dataResult = new DataResult<>();
        } catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }

    @RequestMapping("/findAllUser")
    public DataResult<ShowUser> findAllUser(Integer pageCode) {
        DataResult dataResult = null;
        try {
            PageBean pageBean = new PageBean();
            pageBean.setPageSize(1);
            pageBean.setCurrentPage(pageCode);
            pageBean = mangeUserService.findAllUser(pageBean);
            dataResult = new DataResult(pageBean);
        } catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }

    @RequestMapping("/deleteUser")
    public DataResult deleteUser(User user){
        DataResult dataResult = null;
        try {
            mangeUserService.deleteUser(user.getUId());
            dataResult = new DataResult();
        } catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }

    @RequestMapping("/updateUser")
    public DataResult updateUser(@RequestBody NewUserVo newUserVo){
        DataResult dataResult = null;
        try {
            mangeUserService.updateUser(newUserVo);
            dataResult = new DataResult();
        } catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }

    @RequestMapping("/findUserById")
    public DataResult findUserById(@RequestBody ShowUser user){
        DataResult dataResult = null;
        try {
            NewUserVo user1=mangeUserService.findUserById(user.getUId());
            dataResult = new DataResult(user1);
        } catch (Exception e) {
            if (e instanceof SQLException) {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SQLEXCEPTION);
            } else {
                e.printStackTrace();
                dataResult = new DataResult(ResponseCode.SYSTEMEXCEPTION);
            }
        }
        return dataResult;
    }

}
